package org.example.lethear.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.example.lethear.dto.common.PageResult;
import org.example.lethear.dto.post.*;
import org.example.lethear.service.PostService;
import org.example.lethear.utils.Result;
import org.springframework.web.bind.annotation.*;

/**
 * 帖子管理控制器
 */
@Tag(name = "帖子管理", description = "帖子的增删改查、点赞、评论等相关接口")
@RestController
@RequestMapping("/posts")
@RequiredArgsConstructor
public class PostController {
    
    private final PostService postService;
    
    /**
     * 创建帖子
     */
    @Operation(summary = "创建帖子", description = "发布新的帖子")
    @PostMapping
    public Result<PostDTO> createPost(@Valid @RequestBody PostCreateRequest request) {
        PostDTO post = postService.createPost(request);
        return Result.success("帖子创建成功", post);
    }
    
    /**
     * 更新帖子
     */
    @Operation(summary = "更新帖子", description = "修改已发布的帖子")
    @PutMapping("/{postId}")
    public Result<PostDTO> updatePost(
            @Parameter(description = "帖子ID") @PathVariable Long postId,
            @Valid @RequestBody PostUpdateRequest request) {
        PostDTO post = postService.updatePost(postId, request);
        return Result.success("帖子更新成功", post);
    }
    
    /**
     * 删除帖子
     */
    @Operation(summary = "删除帖子", description = "删除指定的帖子")
    @DeleteMapping("/{postId}")
    public Result<Void> deletePost(@Parameter(description = "帖子ID") @PathVariable Long postId) {
        postService.deletePost(postId);
        return Result.success("帖子删除成功");
    }
    
    /**
     * 获取帖子详情
     */
    @Operation(summary = "获取帖子详情", description = "获取指定帖子的详细信息")
    @GetMapping("/{postId}")
    public Result<PostDTO> getPostById(@Parameter(description = "帖子ID") @PathVariable Long postId) {
        PostDTO post = postService.getPostById(postId);
        return Result.success("获取成功", post);
    }
    
    /**
     * 查询帖子列表
     */
    @Operation(summary = "查询帖子列表", description = "根据条件分页查询帖子")
    @GetMapping
    public Result<PageResult<PostDTO>> getPosts(@Valid PostQueryRequest request) {
        PageResult<PostDTO> posts = postService.getPosts(request);
        return Result.success("查询成功", posts);
    }
    
    /**
     * 获取热门帖子
     */
    @Operation(summary = "获取热门帖子", description = "获取热门帖子列表")
    @GetMapping("/hot")
    public Result<PageResult<PostDTO>> getHotPosts(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") int size) {
        PageResult<PostDTO> posts = postService.getHotPosts(page, size);
        return Result.success("获取成功", posts);
    }
    
    /**
     * 点赞帖子
     */
    @Operation(summary = "点赞帖子", description = "为指定帖子点赞")
    @PostMapping("/{postId}/like")
    public Result<Void> likePost(@Parameter(description = "帖子ID") @PathVariable Long postId) {
        postService.likePost(postId);
        return Result.success("点赞成功");
    }
    
    /**
     * 取消点赞帖子
     */
    @Operation(summary = "取消点赞", description = "取消对指定帖子的点赞")
    @DeleteMapping("/{postId}/like")
    public Result<Void> unlikePost(@Parameter(description = "帖子ID") @PathVariable Long postId) {
        postService.unlikePost(postId);
        return Result.success("取消点赞成功");
    }
    
    /**
     * 获取置顶帖子
     */
    @Operation(summary = "获取置顶帖子", description = "获取置顶帖子列表")
    @GetMapping("/top")
    public Result<PageResult<PostDTO>> getTopPosts(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "5") int size) {
        // 构建置顶帖子查询请求
        PostQueryRequest request = new PostQueryRequest();
        request.setPage(page);
        request.setSize(size);
        request.setSortBy("createTime");
        request.setSortOrder("DESC");
        request.setIsTop(true);
        
        PageResult<PostDTO> posts = postService.getPosts(request);
        return Result.success("获取成功", posts);
    }
    
    /**
     * 根据分类获取帖子
     */
    @Operation(summary = "根据分类获取帖子", description = "获取指定分类下的帖子")
    @GetMapping("/category/{categoryId}")
    public Result<PageResult<PostDTO>> getPostsByCategory(
            @Parameter(description = "分类ID") @PathVariable Long categoryId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") int size,
            @Parameter(description = "排序字段") @RequestParam(defaultValue = "createTime") String sortBy,
            @Parameter(description = "排序方向") @RequestParam(defaultValue = "DESC") String sortOrder) {
        
        PostQueryRequest request = new PostQueryRequest();
        request.setCategoryId(categoryId);
        request.setPage(page);
        request.setSize(size);
        request.setSortBy(sortBy);
        request.setSortOrder(sortOrder);
        
        PageResult<PostDTO> posts = postService.getPosts(request);
        return Result.success("获取成功", posts);
    }
    
    /**
     * 根据作者获取帖子
     */
    @Operation(summary = "根据作者获取帖子", description = "获取指定作者的帖子")
    @GetMapping("/author/{authorId}")
    public Result<PageResult<PostDTO>> getPostsByAuthor(
            @Parameter(description = "作者ID") @PathVariable Long authorId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") int size,
            @Parameter(description = "排序字段") @RequestParam(defaultValue = "createTime") String sortBy,
            @Parameter(description = "排序方向") @RequestParam(defaultValue = "DESC") String sortOrder) {
        
        PostQueryRequest request = new PostQueryRequest();
        request.setAuthorId(authorId);
        request.setPage(page);
        request.setSize(size);
        request.setSortBy(sortBy);
        request.setSortOrder(sortOrder);
        
        PageResult<PostDTO> posts = postService.getPosts(request);
        return Result.success("获取成功", posts);
    }
} 